package com.xianyu.us.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xianyu.us.common.exception.ServiceException;
import com.xianyu.us.common.utils.ShiroUtils;
import com.xianyu.us.pojo.entity.UsPayOrderParam;
import com.xianyu.us.service.UsPayService;
import com.xianyu.us.service.UsUserService;

/**
 * 支付相关业务实现类
 * @author wangshili
 *
 */
@Service
public class UsPayServiceImpl implements UsPayService{
	 private static final Logger log = 
			 LoggerFactory.getLogger(UsPayServiceImpl.class);
	 
	 	/*读取配置文件的信息*/
	    @Value("${ali_return_url}")
	    private String ali_return_url;
	 
	    @Value("${ali_notify_url}")
	    private String ali_notify_url;
	 
	    @Value("${product_code}")
	    private String product_code;
	 
	    @Value("${timeout_express}")
	    private String timeout_express;
	 
	    @Value("${url}")
	    private String url;
	 
	    @Value("${appid}")
	    private String appid;
	 
	    @Value("${private_key}")
	    private String private_key;
	 
	    @Value("${ali_public_key}")
	    private String ali_public_key;
	 
	    @Value("${sign_type}")
	    private String sign_type;
	 
	    @Value("${charset}")
	    private String charset;
	    
	    @Override
	    public String usPay(UsPayOrderParam alipayParam) {
	    	/**
	    	 * 对参数校验
	    	 */
	    	
	    	if(StringUtils.isEmpty(alipayParam.getRecAddr()))
	    		throw new ServiceException("收货地址不能为空");
	    	/*按照支付宝相关的API要求实现*/
	        AlipayClient alipayClient = new DefaultAlipayClient(url, appid, private_key, "json", charset, ali_public_key, sign_type);
	        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
	        alipayRequest.setReturnUrl(ali_return_url);//同步地址,即支付完成后会跳转指定页面
	        /*将额外的参数拼接到异步地址,其他的特定参数支付宝会自动发送，但额外参数需要自己拼接*/
	        String urlNotify =  ali_notify_url+"?"
		        +"sellerId="+alipayParam.getSellerId()+"&" //卖家id
		        +"buyerId="+ShiroUtils.getId()+"&"  //买家id
		        +"commoditysId="+alipayParam.getCommoditysId()+"&"  //商品id
		        +"number="+alipayParam.getNumber()+"&"  //商品数量
		        +"recAddr="+alipayParam.getRecAddr();  //收货地址
	        alipayRequest.setNotifyUrl(urlNotify);  //异步通知，即支付成功后发送请求到后台
	        //AlipayOrderParam alipayOrderParam = new AlipayOrderParam();
//	        alipayOrderParam.setOut_trade_no(alipayParam.getOrderId());//唯一标识
//	        alipayOrderParam.setSubject(alipayParam.getOrderName());
//	        alipayOrderParam.setTotal_amount(alipayParam.getAmount());
	        alipayParam.setProduct_code(product_code);//必须
	        alipayParam.setTimeout_express(timeout_express);//超时时间
	        alipayRequest.setBizContent(JSON.toJSONString(alipayParam));
	        
	        String webForm = "";
	        try {
	        	System.out.println(urlNotify);
	            webForm = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
	            
	        } catch (Exception e) {
	        	log.error("支付请求发送失败");
	        }
	        return webForm;
	    }
}
